const path = require('path')

//这个插件是必须的！ 它的职责是将你定义过的其它规则复制并应用到 .vue 文件里相应语言的块。例如，如果你有一条匹配 /\.js$/ 的规则，那么它会应用到 .vue 文件里的 <script> 块
const VueLoaderPlugin = require('vue-loader/lib/plugin')

function resolve(dir) {
    return path.join(__dirname, '..', dir)
}

module.exports = {
    entry: './src/main.js',
    mode: 'none',
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.(js|vue)$/,
                enforce: 'pre',
                include: [resolve('src'), resolve('test')],
                use: [
                    {
                        loader: 'eslint-loader',
                        options: {
                            formatter: require('eslint-friendly-formatter'),
                            emitWarning: true
                        }
                    }
                ]
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')],
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ["@babel/preset-env"],
                        }
                    }
                ]
            },
            
            {
                test: /\.vue$/,
                use: [
                    {
                        loader: 'vue-loader',
                        options: {
                            loaders: {
                                js: [
                                    { loader: 'babel-loader', options: { presets: ['env'] } }
                                ],
                                css: [
                                    { loader: 'vue-style-loader' },
                                    { loader: 'css-loader' }
                                ],
                                less: [
                                    { loader: 'vue-style-loader' },
                                    { loader: 'css-loader' },
                                    { loader: 'less-loader' }
                                ]
                            },
                            transformToRequire: {
                                video: ['src', 'poster'],
                                source: 'src',
                                img: 'src',
                                image: 'xlink:href'
                            }
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                //先处理css文件 再处理vue中的<style>标签文件
                use: [
                    'style-loader',
                    'css-loader',
                ]
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 10000,
                            esModule: false,//vue 模块请求使用的CommonJS规范，file-loader使用的是ESModule规范，只需要设置ESModule不使用即可正常加载图片
                            name: path.posix.join('static', 'img/[name].[hash:7].[ext]')
                        }
                    }
                ]
            },
        ]
    },
    plugins: [
        // 请确保引入这个插件 处理.vue 中的script标签代码块
        new VueLoaderPlugin(),
    ]
}